<?php
/**
 * Created by xiaosong
 * E-mail:4155433@gmail.com
 * Date: 2019/10/17
 * Time: 11:00
 */

namespace App\Admin\Controllers\config;


use App\Admin\Controllers\config\forms\Create;
use App\Http\Controllers\Controller;
use App\Models\config\ConfigCate;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;

class ConfigsController extends Controller
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '系统配置管理';

    public function index(Content $content)
    {

        $rows = ConfigCate::orderBy('sort')->get();
//        dd(app_path());
//        $filePath = app_path().'\app\Admin\Controllers\config\forms\BaseForm.php';
        $filePath = '..\app\Admin\Controllers\config\forms\BaseForm.php';

        $prefix = '..';
        $suffix = '.php';
        $baseNamespace = '\App\Admin\Controllers\config\forms\BaseForm';

        $forms = [];

        $basePath = $prefix.$baseNamespace.$suffix;

        foreach ($rows as $k => $v){

            $namespace = str_replace('BaseForm',ucwords($v['module']),$baseNamespace);

            $path = $prefix.$namespace.$suffix;

            if (!file_exists($path)){
                $file = file_get_contents($basePath);

                $file = str_replace('default_replace_title',$v['name'],$file);
                $file = str_replace('default_replace_module',$v['module'],$file);
                $file = str_replace('BaseForm',ucwords($v['module']),$file);
                fopen($path, "w") or die("Unable to open file!");
                $myfile = fopen($path, "w");
                fwrite($myfile, $file);
                fclose($myfile);
            }
            $forms[$v['module']] = $namespace;
//            $forms[$v['module']] = Base::class;
        }

        $forms['create'] = Create::class;

        return $content
            ->title('配置信息')
            ->body(Tab::forms($forms));
    }
}